package appapi.controller.oider;

import appapi.entity.Zhongjiao.SelfcheckDto;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.IAppApiService;
import appapi.service.IAppOperatorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Yujie
 * @date 2023/5/11 10:09
 * @description: 操作人员首页
 */
@Api(tags = "操作人员")
@RestController
@RequestMapping(value = "/api/operatorHomePage")
public class AppOperatorController {
    @Resource
    private IAppOperatorService iAppOperatorService;
    @Resource
    private IAppApiService iAppApiService;

    /**
     * 获取操作人员接口
     */
    @ApiOperation(value = "获取操作人员信息", notes = "获取操作人员信息")
    @RequestMapping(value = "/GetOperator", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "oper_id", dataType = "Long", value = "人员ID")
    })
    public ResponseModel GetOperator(long oper_id) {

        ResponseDataModel<List<Map<String, Object>>> response = new ResponseDataModel<>();
        try {
            List<Map<String, Object>> list = iAppOperatorService.GetOperator(oper_id);
            int count = list.size();
            response.setCode(200);
            response.setRcount(count);
            response.setMessage("查询成功");
            response.setData(list);
            return response;
        } catch (Exception ex) {
            response.setCode(400);
            response.setMessage("接口异常" + ex);
            return response;

        }
    }

    /**
     * 新增自检信息接口
     */
    @ApiOperation(value = "新增自检信息接口", notes = "新增自检信息接口")
    @PostMapping(value = "/NewSelfcheck")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "selfcheckDto", dataType = "object", value = "自检信息参数")
    })
    public ResponseModel GetOperator(@Validated SelfcheckDto selfcheckDto) {

        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            int result = iAppOperatorService.InsertSelfcheck(selfcheckDto);
            responseModel.setMessage("新增用户成功");
            return responseModel;
        } catch (Exception ex) {
            responseModel.setMessage("新增用户异常: ".concat(ex.getMessage()));
            responseModel.setCode(400);
            return responseModel;
        }
    }
    /**
    查看自检信息
     */
    @ApiOperation(value = "查看自检信息",notes = "查看自检查信息")
    @GetMapping(value = "/GetSelfcheckList")
    public  ResponseModel GetSelfcheckList(String equip_no,Integer pageNum, Integer pageSize){
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        ResponseDataModel<PageInfo> response=new ResponseDataModel<>();
        try {
            List<Map<String,Object>> list= iAppApiService.GetSelf(equip_no,pageNum,pageSize);
            PageInfo pageInfo=new PageInfo(list,pageSize);
            int count = list.size();
            response.setCode(200);
            response.setRcount(count);
            response.setMessage("查询成功");
            response.setData(pageInfo);
            return response;
        }catch (Exception ex){
            response.setCode(400);
            response.setMessage("接口异常" + ex);
            return response;
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
    }
}
